package com.hlhj.funlike_business.activity.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import com.example.mymvp.mvp.BaseFgm;
import com.flyco.tablayout.SlidingTabLayout;
import com.hlhj.funlike_business.Event.JpushEvent;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.FgmPagerAdp;
import com.hlhj.funlike_business.presenter.OrderFgmPresenter;
import com.hlhj.funlike_business.utils.ViewFindUtils;
import com.hlhj.funlike_business.views.OrderFgmView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class OrderFragment extends BaseFgm<OrderFgmView,OrderFgmPresenter> implements OrderFgmView {
    private FgmPagerAdp fgmPagerAdp;
    private ViewPager order_viewpager;
    private List<Fragment> fgmlist;
    private SlidingTabLayout tabLayout;
    public static int takeoder,alerdyorder;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    tabLayout.showMsg(1,alerdyorder);
                    tabLayout.showMsg(2,takeoder);
                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_order;
    }

    @NotNull
    @Override
    public OrderFgmView bindView() {
        return this;
    }

    @NotNull
    @Override
    public OrderFgmPresenter createPresenter() {
        return new OrderFgmPresenter();
    }

    @Override
    public void initView() {
        order_viewpager = rootView.findViewById(R.id.order_viewpager);
        tabLayout = rootView.findViewById(R.id.tabLayout);
    }

    @Override
    public void logicStart() {
        fgmlist = new ArrayList<>();

        fgmlist.add(new AllOrderFragment());
        fgmlist.add(new WaitOrderFragment());
        fgmlist.add(new WaitTakeFragment());
        fgmlist.add(new CompletedFragment());
        fgmlist.add(new RefundFragment());

        fgmPagerAdp = new FgmPagerAdp(fgmlist,getChildFragmentManager());
        order_viewpager.setAdapter(fgmPagerAdp);
        tabLayout.setViewPager(order_viewpager);

        tabLayout.showDot(0);
        tabLayout.setTextSelectColor(getContext().getResources().getColor(R.color.tvblue));
        tabLayout.setTextUnselectColor(getContext().getResources().getColor(R.color.black));
        tabLayout.setIndicatorColor(getContext().getResources().getColor(R.color.tvblue));
        tabLayout.setIndicatorWidth(30);
        tabLayout.showMsg(1,alerdyorder);
        tabLayout.showMsg(2,takeoder);
    }

    @Override
    public void setListener() {

    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void refrsh(String msg){
        Log.e("zy","待接单：" + alerdyorder);
        Log.e("zy","待配送：" + takeoder);
       handler.sendEmptyMessage(1);
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void refresh(JpushEvent jpushEvent){
        Log.e("zy","lalalalalla");
        handler.sendEmptyMessage(1);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
